// 导入electron
const electron = require('electron');
// 导入控制生命周期的模块、创建原生浏览器窗口的模块
const {ipcRenderer} = electron;
// 导入命令行模块
const exec = require('child_process').exec;
const notifier = require('node-notifier');
const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;


class Index {
    constructor() {
    	this.closeWindow_btn = $('.close-window');
    	this.smallWindow_btn = $('.small-window');
    }

    init() {
    	this.ready();
    }

    ready() {
    	this.closeWindow();
        this.smallWindow();
        this.holder();
    }
    // 关闭窗口
    closeWindow() {
    	this.closeWindow_btn.on('click', () => {
			ipcRenderer.send('close-window');    			
    	});
    }
    // 最小化窗口
    smallWindow() {
    	this.smallWindow_btn.on('click', () => {
    		ipcRenderer.send('small-window');
    	});
    }
    holder() {
        let holder = document.getElementById('holder');
        holder.ondragover = function () {
            return false;
        };
        holder.ondragleave = holder.ondragend = function () {
            return false;
        };
        holder.ondrop = function (e) {
            e.preventDefault();
            let obj = $('.project-list');
            let len = obj.find('.list-item').length;
            if (len < 2) {
                var file = e.dataTransfer.files[0];
                if (file.path) {
                    notifier.notify({
                        'title': '提醒咯 ～',
                        'subtitle': '副标题',
                        'icon': '' + process.cwd() + '/assets/img/WeFlow.png',
                        'message': '新增项目目录:'+file.path+'',
                        'contentImage': 'file://' + process.cwd() + '/assets/img/WeFlow.png',
                        time: 3000,
                        sound: true,
                        wait: true
                    });

                    notifier.on('click', function (notifierObject, options) {
                        var win = new BrowserWindow({ width: 800, height: 500 });
                        win.on('closed', function() {
                          win = null;
                        });
                        win.webContents.openDevTools();
                        
                        win.loadURL('file://' + process.cwd() + '/app/about.html');

                    }); 

                    obj.append('<div class="list-item"><i class="iconfont">&#xe6f0;</i><p>'+file.path+'</p><i class="iconfont">&#xe732;</i></div>');
                    obj.find('.list-item').on('click', () => {
                        let path = ''+$(this).find('p').text()?$(this).find('p').text(): ''+'';
                        console.log(path);
                        exec('ls /Users/6ceng/mps-app', (error, stdout, cwd) => {
                           if (error) {
                             console.error(error);
                             return;
                           }
                            console.log(stdout);
                            console.log(cwd);
                        });                        
                    });
                }
            }
            if (len == 1) {
                $(holder).addClass('hide');
            }
            return false;
        };
    }
}

new Index().init();


module.exports = global.wins;



